package com.bk.service.userInfo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bk.entity.CardInfo;
import com.bk.entity.UserInfo;
import com.bk.mapper.cardInfo.CardInfoMapper;
import com.bk.mapper.userInfo.UserInfoMapper;
import com.bk.util.ReturnUtil;
import com.bk.vo.UserCardInfoVo;

@Service
public class UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private CardInfoMapper cardInfoMapper;

	/**查询账户信息*/
	public ReturnUtil<UserInfo> queryAccountInfo(String cardId) {
		CardInfo cardInfo=cardInfoMapper.isExistCardByAccountId(cardId);
		if(null!=cardInfo) {
			UserInfo userInfo=userInfoMapper.queryAccountInfoByCardId(cardId);
			userInfo=getUhome(userInfo);//截取逗号
			List<UserInfo> userInfoList=new ArrayList<UserInfo>();
			if(null!=userInfo) {
				userInfoList.add(userInfo);
				return new ReturnUtil<UserInfo>(200,"用户信息返回成功",userInfoList);
			}else {
				return new ReturnUtil<UserInfo>(400,"该账户用户信息不存在",null);
			}
		}else {
			return new ReturnUtil<UserInfo>(400,"该账户不存在或已被冻结",null);
		}
		
	}
	
	/** 获取所有客户信息 */
	public ReturnUtil<UserInfo> queryUserInfoList() {
		List<UserInfo> userInfoList=userInfoMapper.queryUserInfoList();
		return userInfoList.size()>0?new ReturnUtil<UserInfo>(200,"用户信息返回成功",userInfoList):new ReturnUtil<UserInfo>(400,"用户信息返回失败",null);
	}

	/**根据账户名和身份证查询客户信息以及开户账号*/
	public ReturnUtil<UserCardInfoVo> getUserInfoByUidAndName(String uName, String uId) {
		UserInfo existUserInfo = userInfoMapper.isExistUserInfo(uId, uName);
		if(existUserInfo!=null) {
			existUserInfo=getUhome(existUserInfo);//截取逗号
			List<UserCardInfoVo> luv=new ArrayList<>(); 
			UserCardInfoVo uv=new UserCardInfoVo();
			uv.setUserInfo(existUserInfo);
			List<CardInfo> cardInfoList=cardInfoMapper.getAccountId(uId);//获取客户的多张卡号
			if(cardInfoList.size()>0) {
				uv.setCardInfoList(cardInfoList);
			    luv.add(uv);
			    return cardInfoList.size()>0?new ReturnUtil<UserCardInfoVo>(200,"客户账户信息返回成功",luv):new ReturnUtil<UserCardInfoVo>(400,"客户账户信息返回失败",null);
			}else {
				return new ReturnUtil<UserCardInfoVo>(200,"当前还未客户还未创建卡号",null);
			}
		}else {
			return new ReturnUtil<UserCardInfoVo>(400,"账户名或身份证信息有误",null);
		}
	}
	/**按照逗号截取地址*/
	public UserInfo getUhome(UserInfo userInfo) {
		String uhome = userInfo.getUhome();
		String[] homeArrs = uhome.split(",");
		String userHome="";
		for (String newHome : homeArrs) {
			userHome=userHome+newHome;
		}
		userInfo.setUhome(userHome);
		return userInfo;
	}
}
